<?php
/**
**名称:静态化管理
**作用:天猫折扣精品库的静态化
**作者:张剑 swordphp@126.com
**创建日期:2013-07-29
**最后修改日期:2013-07-29
**/
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
require_once("./phpcms/modules/sgw_jizi/index.php");
class static_page extends admin{
	public function __construct(){
		$this->index = new index;
		pc_base::load_sys_func('global');
	}
	public function static_index(){
		$file = './swwwroot/index.html';
		ob_start();
		$this->index->init();
		$data = ob_get_contents();
		$data = str_replace($this->img_root,'/uploadfile/',$data);
		$data = str_replace(APP_PATH,'/',$data);
		$data = compress_html($data);
		$size = $this->save_page($file,$data);
		ob_clean();
		$this->static_list();
		$size = round(($size/1024),2);
		showmessage(L("手机版首页及其子页面生成完毕,新生成手机版首页大小{$size}KB",''));
	}
	public function static_list(){
		$pid_info = $this->index->jizi_db->get_one(array('short_name'=>'jizi_index'));
		$infos = $this->index->jizi_db->select(array('pid'=>$pid_info['id'],'is_display'=>1),'*','','order_id ASC');
		foreach($infos as $val){
			$file = "./swwwroot/".$val['short_name']."/index.html";
			ob_start();
			$_GET['sort'] = $val['short_name'];
			$this->index->ji_sort();
			$data = ob_get_contents();
			$data = compress_html($data);
			$this->save_page($file,$data);
			ob_clean();
		}
	}
	private function save_page($file,$data) {
		$dir = dirname($file);
		if(!is_dir($dir)) {
			@mkdir($dir, 0777,1);
		}
		$strlen = file_put_contents($file, $data);
		chmod($file,0777);
		if(!is_writable($file)) {
			$file = str_replace(PHPCMS_PATH,'',$file);
			showmessage(L('file').'：'.$file.'<br>'.L('not_writable'));
		}
		return $strlen;
	}
}